<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


require_once 'Classes/Model/PDOModel.php';
require_once 'Classes/Structure.php';
class StructureModel extends PDOModel implements Singleton

{
 
     private static $instance;
     
     public function getInstance(){
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
     }
     
     protected function __construct() {
        parent::__construct();
    }
     
     public function __clone() {
         throw new Exception("You may not try to clone this object");
     }
 
    
    public function ListStructures(){       
         $sql = "SELECT * FROM Structures";       
         $this->PDORequest($sql,array($Name, $Address));
    }
    
    public function getAllStructures() {
        $result = $this->PDORequest('SELECT * FROM Structures');
           
        if($result) {
            $arr = array();
            foreach ($result as $structure) {
                array_push($arr, new Structure($structure));
            }
            return $arr;
        }
    }
    
    public function AddStructure($structure){
         $sql = "INSERT INTO Structures (Name, Address) VALUES (?, ?)";
             
         $this->PDORequest($sql,array($structure->getName(), $structure->getAddress()));
    }
    
    public function EditStructure($structure){
         $sql = "UPDATE Structures SET Name=?, Address=? WHERE ID = ? ";
         $this->PDORequest($sql,array($structure->getName(), $structure->getAddress(), $structure->getID()));
    }
     
    
    public function DeleteStructureByName($Name){
         $sql = "DELETE FROM Structures WHERE Structures.Name = ? ";
         $this->PDORequest($sql,array($Name));
    }
    
    public function DeleteStructureById($id){
         $sql = "DELETE FROM Structures WHERE Structures.ID = ? ";
         $this->PDORequest($sql,array($id));
    }
    
    public function GetStructureById($id){
        $sql = "SELECT * FROM Structures WHERE ID = ? ";
        $dbData = $this->PDORequest($sql,array($id));
        return new Structure($dbData[0]);
    }
    
}
?>
